package com.sitech.openapi.controller;

import com.sitech.openapi.componet.SetHttpHeaderCmpt;
import com.sitech.openapi.domain.*;
import com.sitech.openapi.service.InvoiceInfoService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.context.request.NativeWebRequest;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Api(tags = "电子发票的开具及查询等相关信息",description = "电子发票相关信息的控制层")
@RestController
public class InvoiceInfoController {

    @Autowired
    InvoiceInfoService invoiceInfoService;
    @Autowired
    private SetHttpHeaderCmpt setHttpHeaderCmpt;

    @GetMapping("/orderOneItem")
    @ApiOperation(value = "查询营业订单类型的增值税普通电子发票",notes = "营业订单API")
    public ResponseEntity<List<OrderOneItem>> getCustOrders(@RequestParam String serialNumber,@RequestParam String lanId,@RequestHeader HttpHeaders headers){

        List<OrderOneItem> orderOneItems = invoiceInfoService.getOrderOneItems(serialNumber,lanId);
        HttpHeaders httpHeaders = setHttpHeaderCmpt.setOKHttpHeaders(headers);
        ResponseEntity<List<OrderOneItem>> resp = new ResponseEntity<List<OrderOneItem>>(orderOneItems,httpHeaders, HttpStatus.OK);
        return  resp;
    }

    @PostMapping("/invoiceInformation")
    @ApiOperation(value = "新增开票信息", notes = "开票信息API")
    public ResponseEntity<InvoiceInformation> addInvoiceInfo(@RequestBody InvoiceInformation invoiceInformation,
                                                              @RequestHeader HttpHeaders headers){
        InvoiceInformation invoiceInformation0 = invoiceInfoService.addInvoiceInfo(invoiceInformation);
        HttpHeaders httpHeaders = setHttpHeaderCmpt.setOKHttpHeaders(headers);
        ResponseEntity<InvoiceInformation> resp = new ResponseEntity<InvoiceInformation>(invoiceInformation0,httpHeaders, HttpStatus.OK);
        return  resp;
    }

    @GetMapping("/invoiceInformation")
    @ApiOperation(value = "查询开票信息",notes = "开票信息API")
    public ResponseEntity<InvoiceInformation> getInvoiceInfo(@RequestParam String serialNumber,
                                                             @RequestParam String type,
                                                             @RequestParam String lanId,
                                                             @RequestHeader HttpHeaders headers){

        InvoiceInformation invoiceInformation = invoiceInfoService.getInvoiceInfo(serialNumber,type,lanId);
        HttpHeaders httpHeaders = setHttpHeaderCmpt.setOKHttpHeaders(headers);
        ResponseEntity<InvoiceInformation> resp = new ResponseEntity<InvoiceInformation>(invoiceInformation,httpHeaders, HttpStatus.OK);
        return  resp;
    }

    @PatchMapping("/invoiceInformation/{id}")
    @ApiOperation(value = "修改开票信息", notes = "开票信息API")
    public ResponseEntity<InvoiceInformation> updateInvoiceInfo(@PathVariable String id,
                                                                @RequestBody List<Map<String,String>> attrInfoList,
                                                                @RequestHeader HttpHeaders headers) throws Exception {
        InvoiceInformation invoiceInformation = invoiceInfoService.updateInvoiceInfo(id, attrInfoList);
        HttpHeaders httpHeaders = setHttpHeaderCmpt.setOKHttpHeaders(headers);
        ResponseEntity<InvoiceInformation> resp = new ResponseEntity<InvoiceInformation>(invoiceInformation,httpHeaders, HttpStatus.OK);
        return  resp;
    }

    @PostMapping("/invoice")
    @ApiOperation(value = "新增开票",notes = "开具完成后的增值税普通电子发票的异步返回——发票API")
    public ResponseEntity<String> getIssueInvoiceResult(@RequestBody Invoice invoice,@RequestHeader HttpHeaders headers){

        HttpHeaders httpHeaders = setHttpHeaderCmpt.setOKHttpHeaders(headers);
        ResponseEntity<String> resp = new ResponseEntity<String>("200",httpHeaders, HttpStatus.OK);
        return  resp;
    }

    @GetMapping("/invoice")
    @ApiOperation(value = "发票查询，如果已开具发票，则调用该接口返回",notes = "通过发票实例ID查询电子发票的信息——发票API")
    public ResponseEntity<Invoice> getInvoiceById(@RequestParam String invoiceId,@RequestParam String lanId,@RequestHeader HttpHeaders headers){

        Invoice invoice = invoiceInfoService.getInvoiceById(invoiceId,lanId,headers);
        HttpHeaders httpHeaders = setHttpHeaderCmpt.setOKHttpHeaders(headers);
        ResponseEntity<Invoice> resp = new ResponseEntity<Invoice>(invoice,httpHeaders, HttpStatus.OK);
        return  resp;
    }

    @PostMapping("/issueInvoice")
    @ApiOperation(value = "开电子发票",notes = "开具发票API")
    public ResponseEntity<String> doIssueInvoice(@RequestBody IssueInvoice issueInvoice,@RequestHeader HttpHeaders headers){

        invoiceInfoService.doIssueInvoice(issueInvoice,headers);
        HttpHeaders httpHeaders = setHttpHeaderCmpt.setOKHttpHeaders(headers);
        ResponseEntity<String> resp = new ResponseEntity<String>("",httpHeaders, HttpStatus.OK);
        return  resp;
    }
    @GetMapping("/test")
    @ApiOperation(value = "主要是用于测试",notes = "测试")
    public ResponseEntity<String> test(@RequestParam String a,@RequestHeader HttpHeaders headers){
        invoiceInfoService.test();
        String returnCode = "200";
        HttpHeaders httpHeaders = setHttpHeaderCmpt.setOKHttpHeaders(headers);
        ResponseEntity<String> resp = new ResponseEntity<String>(returnCode,httpHeaders, HttpStatus.OK);
        return  resp;
    }


    @ExceptionHandler
    public ResponseEntity<Map<String, String>> ContactTaskError(Exception e, NativeWebRequest request) {

        Map<String, String> errors = new HashMap<>();
        String requestId = request.getHeader("X-CTG-Request-ID");
        errors.put("code", HttpStatus.BAD_REQUEST.toString());
        errors.put("reason", e.getMessage());
        errors.put("message",e.toString());
        errors.put("referenceError","");

        HttpHeaders httpHeaders = new HttpHeaders();
        httpHeaders.set("X-CTG-Request-ID",requestId);
        ResponseEntity<Map<String, String>> re = new ResponseEntity<>(errors, httpHeaders, HttpStatus.BAD_REQUEST);
        return re;
    }


}
